<?php
namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Session;

class Lists extends Controller
{
    public function getIndex()
    {
        $request=$this->request;
        $res=$request->param();
        $id=$res['id'];
        $list_datas=Db::query("SELECT * FROM tp_goods WHERE `goods_status`=? and goods_class in(select id  from tp_category WHERE pid=?) or goods_class=?",[0,$id,$id]);

        $j=0;
        foreach($list_datas as $v)
        {
            $list_datas[$j]['goods_pic']='/uploads/goods_pic/'.$v['goods_pic'];
            $j++;
        }
        //获取数据的总条数
        $totalpage=count($list_datas);
        //每页显示条数
        $rev=1;
        //获取最大页
        $maxpage=ceil($totalpage/$rev);
        $pp=array();
        for($i=1;$i<=$maxpage;$i++)
        {
            $pp[$i]=$i;
        }
        //获取偏移量
        $list_datas=array_slice($list_datas,0,$rev);
        $this->assign('maxpage',$maxpage);
        $this->assign('id',$id);
        $this->assign('pp',$pp);
        $this->assign('list_datas',$list_datas);
        return $this->fetch('lists/catalog_grid');
    }
    //分页
    public function getpage()
    {
        $request=$this->request;
        $res=$request->param();
        $page=$res['page'];

        $id=$res['id'];
        $list_datas=Db::query("SELECT * FROM tp_goods WHERE `goods_status`=? and goods_class in(select id  from tp_category WHERE pid=?) or goods_class=?",[0,$id,$id]);
        $j=0;
        foreach($list_datas as $v)
        {
            $list_datas[$j]['goods_pic']='/uploads/goods_pic/'.$v['goods_pic'];
            $j++;
        }
        //获取数据的总条数
        $totalpage=count($list_datas);
        //每页显示条数
        $rev=1;
        //获取最大页
        $maxpage=ceil($totalpage/$rev);
        $pp=array();
        for($i=1;$i<=$maxpage;$i++)
        {
            $pp[$i]=$i;
        }
        //获取偏移量
        $offset=($page-1)*$rev;
        $list_datas=array_slice($list_datas,$offset,$rev);
        $this->assign('list_datas', $list_datas);
        return $this->fetch('lists/page');

       // echo '1';
    }

    //商品详情页
    public  function getdetail()
    {
        $request=$this->request;
        $res=$request->param();
        $detail_datas=Db::table('tp_goods')->alias('gs')
        ->where('gs.id',$res['id'])
        ->find();
        $detail_datas['goods_pic']='/uploads/goods_pic/'.$detail_datas['goods_pic'];
        $brand_datas=Db::table('tp_brand')->where('id',$detail_datas['goods_brand'])->find();
        $brand_datas['brand_pic']='/uploads/brand_pic/'.$brand_datas['brand_pic'];
        //只查询型号
        $model=Db::table('tp_goods')->alias('tg')
            ->join('tp_goods_model tgm','tg.id = tgm.gid')
            ->join('tp_models tm','tgm.mid = tm.id')
            ->where('tg.id',$res['id'])
            ->select();
        $mode_datas=Db::name('goods')->where('id',$res['id'])->find();

        //评论
        $comment_datas=Db::table('tp_comments')->alias('tc')
            ->join('tp_user tu','tc.user_id=tu.id','left')
            ->where('goods_id',$res['id'])
            ->select();
        $j=0;
        foreach($comment_datas as $v)
        {
            $comment_datas[$j]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $j++;
        }

        //添加评论
//            $user_id=Session::get('userinfo')['user_id'];
//            $comt=Db::table('tp_user')->where('id',$user_id)->find();
////            echo '<pre>';
////            print_r($comt);
////            exit;
//            $comt['goods_id']=$res['id'];

            //echo '<script>alert("你还没有登录，请先登录");location="/home/login/index"</script>';



        //$this->assign('comt', $comt);
        $this->assign('comment_datas',$comment_datas);
        $this->assign('brand_datas',$brand_datas);
        $this->assign('detail_datas',$detail_datas);
        $this->assign('mode_datas',$mode_datas);
        $this->assign('model',$model);
        return $this->fetch('lists/product_page');
    }

}
?>